package com.yuncheng.spcyApi.controller;

import java.util.*;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;

import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.spcyApi.entity.SysUserCyyj;
import com.yuncheng.spcyApi.service.ISysUserCyyjService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import utils.LogUtils;
import utils.ObjectToMapUtils;

/**
 * @Description: 系统-用户常用意见库
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Api(tags = "系统-用户常用意见库接口")
@RestController
@RequestMapping("/api/spzx-spcy/sysUserCyyj")
public class SysUserCyyjController extends BaseController<SysUserCyyj, ISysUserCyyjService> {

    private static final Logger log = LoggerFactory.getLogger(SysUserCyyjController.class);

    @Resource
    private ISysUserCyyjService sysUserCyyjService;

    @Resource
    private ISpcyLogService spcyLogService;


    @Log(title = "系统-用户常用意见库-分页列表查询")
    @ApiOperation(value = "系统-用户常用意见库-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(SysUserCyyj sysUserCyyj,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        String userid = "";
        if (currentUser == null){
            return HttpResult.error("登录已过期，请重新登录");
        }
        userid = currentUser.getId();
        Page<SysUserCyyj> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SysUserCyyj> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserCyyj::getFuserid, userid);
        queryWrapper.eq(StringUtils.isNotBlank(sysUserCyyj.getFtype1()),SysUserCyyj::getFtype1, sysUserCyyj.getFtype1());
        queryWrapper.eq(StringUtils.isNotBlank(sysUserCyyj.getFtype2()),SysUserCyyj::getFtype2,sysUserCyyj.getFtype2());
        queryWrapper.eq(StringUtils.isNotBlank(sysUserCyyj.getFtype3()),SysUserCyyj::getFtype3,sysUserCyyj.getFtype3());
        queryWrapper.eq(StringUtils.isNotBlank(sysUserCyyj.getFtype4()),SysUserCyyj::getFtype3,sysUserCyyj.getFtype4());
        queryWrapper.like(StringUtils.isNotBlank(sysUserCyyj.getFcontent()),SysUserCyyj::getFcontent,sysUserCyyj.getFcontent());
        IPage<SysUserCyyj> pageList = sysUserCyyjService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }


    @Log(title = "系统-用户常用意见库-添加")
    @ApiOperation(value = "系统-用户常用意见库-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody SysUserCyyj sysUserCyyj) {
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null) {
            sysUserCyyj.setId(GetUuIdUtils.ReplaceUuId());
            sysUserCyyj.setFuserid(currentUser.getId());

            List<SysUserCyyj> yjList = sysUserCyyjService.list(new LambdaQueryWrapper<SysUserCyyj>()
                    .eq(SysUserCyyj::getFuserid, currentUser.getId())
                    .eq(StringUtils.isNotBlank(sysUserCyyj.getFtype1()), SysUserCyyj::getFtype1,sysUserCyyj.getFtype1())
                    .eq(SysUserCyyj::getFcontent, sysUserCyyj.getFcontent()));
            if (CollectionUtils.isNotEmpty(yjList)){
                return HttpResult.error("该意见已经存在了。");
            }
            sysUserCyyjService.save(sysUserCyyj);

            String lognr = "新增意见: ";
            Map<String, Object> map = ObjectToMapUtils.objectToMap(sysUserCyyj);
            lognr += LogUtils.MapToStringMethod(map);
            spcyLogService.addLogs(LogConstant.LOGS_SYS_USER_CYYJ,lognr,sysUserCyyj.getId(),LogConstant.LOGS_SYS_USER_CYYJ,SpcyConstant.LOGS_SYS_TYPE_XZ);
            return HttpResult.ok("添加成功");
        }else {
            return HttpResult.error("登录已过期，请重新登录，添加失败");
        }
    }

    @Log(title = "系统-用户常用意见库-修改")
    @ApiOperation(value = "系统-用户常用意见库-修改")
    @PostMapping(value = "edit")
    public HttpResult edit(@RequestBody SysUserCyyj bean){
        if (StringUtils.isBlank(bean.getId())){
            return HttpResult.error("参数为空！");
        }
        if (StringUtils.isBlank(bean.getFcontent())){
            return HttpResult.error("内容为空！");
        }
        LambdaUpdateWrapper<SysUserCyyj> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUserCyyj::getId, bean.getId());

        SysUserCyyj updateBean = new SysUserCyyj();
        updateBean.setFcontent(bean.getFcontent());
        sysUserCyyjService.update(updateBean, updateWrapper);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "系统-用户常用意见库-批量删除")
    @ApiOperation(value = "系统-用户常用意见库-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(",|，"));
        if (CollectionUtils.isNotEmpty(idList)){
            for (String id : idList){
                SysUserCyyj cyyj = sysUserCyyjService.getById(id);

                if(cyyj == null){
                    continue;
                }
                sysUserCyyjService.removeById(id);

                String lognr = "删除意见: ";
                Map<String, Object> map = ObjectToMapUtils.objectToMap(cyyj);
                lognr += LogUtils.MapToStringMethod(map);
                spcyLogService.addLogs(LogConstant.LOGS_SYS_USER_CYYJ,lognr,id,LogConstant.LOGS_SYS_USER_CYYJ,SpcyConstant.LOGS_SYS_TYPE_SC);
            }
        }
        return HttpResult.ok("删除成功");
    }


//    @Log(title = "获取当前用户常用意见-根据类型")
//    @ApiOperation(value = "获取当前用户常用意见-根据类型")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(name = "type",value = "意见类型",dataType = "String")
//    })
//    @GetMapping(value = "/getNowUserCyyj")
    public HttpResult<?> getNowUserCyyj(@RequestParam(value = "type",defaultValue = "")String type,
                                        @RequestParam("fcontent")String fcontent){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        List<SysUserCyyj> userCyyjList = new ArrayList<>();
        if (currentUser != null) {
            userCyyjList  = sysUserCyyjService.list(new LambdaQueryWrapper<SysUserCyyj>()
                    .eq(SysUserCyyj::getFuserid, currentUser.getId())
                    .eq(StringUtils.isNotEmpty(type), SysUserCyyj::getFtype1, type)
                    .like(StringUtils.isNotBlank(fcontent),SysUserCyyj::getFcontent,fcontent));
        }

        return HttpResult.ok(userCyyjList);
    }
}
